//done
package GUI;

import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import Client.ConnectionToServer;
import Entities.Type;

/**
 * This Class controls Add Product to Type Window. 
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class AddProductToTypeWindow extends TypeManagementWindow {
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;
	
	/** The product ID Label. */
	private JLabel productIDLabel = null;
	
	/** The product ID ComboBox. */
	private JComboBox productIDComboBox = null;
	
	/** The addflag Variable. */
	private boolean addflag;
	
	/** The remove flag Variable. */
	private boolean removeflag;
	
	/** The product type. */
	private Type type = null; 
	
	/** The Background Label. */
	private JLabel BackgroundLabel;
	
	/**
	 * Default constructor
	 */
	public AddProductToTypeWindow(){
		super();
		setBackground(SystemColor.activeCaption);
		getSectionsIDLabel().setText("Categories");
		initialize(); 
	}
	
	/**
	 * This function initializes AddProductToTypeWindow
	 */
	private void initialize() {
		type = new Type(0,"");
		addflag = false;
		removeflag = false;
		this.setSize(600, 250);
		updateForm();
		getSaveButton().setText("Add");	
		getIdLabel().setText("Type ID");
		getNameLabel().setText("Product ID");
		getTypesIDLabel().setText("Types");
		getProductsIDLabel().setText("All product:");
		getAddbuttonLabel().setText("Selected info:");
		getIdTextField().setText("");
		getNameTextField().setText("");
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		getUpdateButton().setEnabled(false);
		productIDLabel = new JLabel();
		productIDLabel.setBounds(new Rectangle(425, 20, 130, 22));
		productIDLabel.setText("All Products in this Type:");
		addSaveButton();
		addremoveButton();
		addTypesIDComboBox();
		getProductsIDTextAreaScrollPane().setBounds(425, 40, 160, 160);
		this.add(productIDLabel, null);
		this.add(getProductIDComboBox(), null);
		
		BackgroundLabel = new JLabel("");
		BackgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
		BackgroundLabel.setBounds(-13, -471, 628, 721);
		this.add(BackgroundLabel);
		
	}
	
	/**
	 * This function update form comboBoxes to data from DB
	 */
	public void updateForm(){
		updateTypesIDComboBox();
		updateCategoriesIDComboBox();
	}
	
	/**
	 * This function initializes typesIDComboBox
	 */
	private void addTypesIDComboBox(){
		getTypesIDComboBox().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				String str = (String)getTypesIDComboBox().getSelectedItem();
				if (str.equals(":"))
					return;
				String typeID = str.substring(0, str.indexOf(":"));
				getIdTextField().setText(typeID);				
				String[] productslist = null;
				try{
					ConnectionToServer connectiontoserver = new ConnectionToServer();		
					productslist = (String[]) connectiontoserver.loadTypeProduct(Integer.parseInt(typeID));
					if (productslist == null){
						getProductsIDTextArea().setText("");
					}							
					else{
						String tmpstr = "";
						int len = productslist.length;
						for(int i=0; i < len; i++){
							tmpstr = tmpstr+(productslist[i]+"\n");
						}
						getProductsIDTextArea().setText(tmpstr);
					}
				}
				catch(Exception except){
					JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
			}
		});		
	}
	
	/**
	 * This function initializes typesIDComboBox	
	 */
	public JComboBox getProductIDComboBox(){
		if (productIDComboBox == null) {
			productIDComboBox = new JComboBox();
			productIDComboBox.setBounds(new Rectangle(290, 40, 127, 25));
			long[] productid = null;
			try{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				productid = (long[]) connectiontoserver.loadAllProductsID();
				if (productid == null){
					JOptionPane.showMessageDialog(null, "Catalogue is EMPTY!!!");
					productIDComboBox.addItem(0);
				}	
				else{
					int len = productid.length;
					for(int i=0; i < len; i++){
						productIDComboBox.addItem(productid[i]);
					}	
				}
			}
			catch(Exception e){
				productIDComboBox.addItem(0);
			}			
			
			productIDComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					getNameTextField().setText(String.valueOf((Long)productIDComboBox.getSelectedItem()));
				}
			});	
		}
		return productIDComboBox;
	}
	
	/**
	 * This function initializes SaveButton ActionListener
	 */
	private void addSaveButton(){
		getSaveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				if (addflag){
					saveProductToType();										
				}
				else{
					getSaveButton().setText("OK");
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					getAddbuttonLabel().setText("Add this product to the selected type:");
					getRemoveButton().setEnabled(false);
					addflag = true;
				}
			}
		});		
	}
	
	/**
	 * This function adds selected product to the selected type
	 */
	private void saveProductToType(){
		int typeID = 0;
		long productID = 0;
		try{
			typeID = Integer.parseInt(getIdTextField().getText());
			productID = Long.parseLong(getNameTextField().getText());
		}
		catch(Exception exc){
		}
		
		if (typeID > 0 && productID > 0){
			type.setTypeID(typeID);
			try {
				if (type.addProductToType(productID)){
					JOptionPane.showMessageDialog(null, "Product: "+productID+" successfuly added to type: "+typeID);
					restore();
				}
				else
					JOptionPane.showMessageDialog(null, "Produt already exists !","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (Exception e1) {
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}						
		}
		else{
			JOptionPane.showMessageDialog(null, "PLZ select type and product","",JOptionPane.WARNING_MESSAGE);
			restore();
		}
	}
	
	/**
	 * This function initializes removeButton ActionListener
	 */
	private void addremoveButton(){
		getRemoveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
				if (removeflag){
					removeProductFormType();										
				}
				else{
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					getAddbuttonLabel().setText("Remove this product from the type:");
					getSaveButton().setEnabled(false);
					removeflag = true;
				}				
			}
		});		
	}
	
	/**
	 * This function removes selected product from the selected type
	 */
	private void removeProductFormType(){
		int typeID = 0;
		long productID = 0;
		try{
			typeID = Integer.parseInt(getIdTextField().getText());
			productID = Long.parseLong(getNameTextField().getText());
		}
		catch(Exception exc){
		}
		
		if (typeID > 0 && productID > 0){
			type.setTypeID(typeID);
			try{
				if (type.removeProductFromType(productID)){
					JOptionPane.showMessageDialog(null, "Product: "+productID+" successfuly remove from type: "+typeID);
					restore();
				}
				else
					JOptionPane.showMessageDialog(null, "Product not remove!!!","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (HeadlessException e1) {
				JOptionPane.showMessageDialog(null, "System ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			
			} 
			catch (Exception e1) {
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "PLZ select type and product","",JOptionPane.WARNING_MESSAGE);
			restore();
		}
	}
	
	/**
	 * This function restores the form button  
	 */
	public void restore(){
		getSaveButton().setText("Add");	
		getAddbuttonLabel().setText("Selected info:");	
		addflag = false;
		removeflag = false;
		getIdTextField().setForeground(Color.black);
		getNameTextField().setForeground(Color.black);
		getAddbuttonLabel().setForeground(Color.black);
		getIdTextField().setText("");
		getNameTextField().setText("");
		getRemoveButton().setEnabled(true);
		getSaveButton().setEnabled(true);		
	}
}
